package com.dys.test01;

import java.net.*;
import java.util.Enumeration;

/**
 * @className: com.dys.test01.InetAddressTest
 * @description: InetAddress的一个实例由一个IP地址和可能的相应主机名组成（取决于它是用主机名构造还是已经完成了反向主机名解析）。
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/08/20 下午10:47
 */
public class InetAddressTest {
    /**
     * 这是一个main()方法,是应用程序的入口
     *
     * @param args 方法的形参列表,是一个字符串数组,包含了从命令行传递给程序的参数
     */
    public static void main(String[] args) throws UnknownHostException {
        // 不能直接创建对象，因为InetAddress()被default修饰了。
        // InetAddress ia = new InetAddress();
        // InetAddress ia = InetAddress.getLocalHost();
        // System.out.println(ia);
        // 封装域名
        InetAddress ia = InetAddress.getByName("www.baidu.com");
        System.out.println(ia);
        // 获取域名
        String hostName = ia.getHostName();
        System.out.println("获取域名：" + hostName);
        String hostAddress = ia.getHostAddress();
        System.out.println("获取域名绑定的IP: " + hostAddress);

        // localhost指代的是本机的ip地址
        InetAddress ia2 = InetAddress.getByName("localhost");
        System.out.println("本机的ip地址：" + ia2);

        // 127.0.0.1指代的是本机的ip地址
        InetAddress ia3 = InetAddress.getByName("127.0.0.1");
        System.out.println("获取本机的ip地址：" + ia3);

        InetAddress ip = InetAddress.getByName("192.168.3.5");
        System.out.println("本机ipv4地址：" + ip);

        // 封装计算机名词
        InetAddress ia4 = InetAddress.getByName("pinecone");
        System.out.println("通过计算机名字获取本机的ip: " + ia4);
    }

    /**
     * 多网卡指定名字类型返回指定InetAddress
     * 输入类型错误则输出全部网卡接口信息
     *
     * @param Name 名字
     * @param type 类型:  4--Inet4Address  6--Inet6Address
     * @return
     */
    public static InetAddress getInetAddress(String Name, int type) {
        try {
            Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
            InetAddress addr;
            if (type == 4) {
                while (nifs.hasMoreElements()) {
                    NetworkInterface nif = nifs.nextElement();
                    Enumeration<InetAddress> addresses = nif.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        addr = addresses.nextElement();
                        if (nif.getName().equals(Name) && addr instanceof Inet4Address) {
                            return addr;
                        }
                    }
                }
            } else if (type == 6) {
                while (nifs.hasMoreElements()) {
                    NetworkInterface nif = nifs.nextElement();
                    Enumeration<InetAddress> addresses = nif.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        addr = addresses.nextElement();
                        if (nif.getName().equals(Name) && addr instanceof Inet6Address) {
                            return addr;
                        }
                    }
                }
            } else {
                System.out.println("类型指定错误,将输出全部网卡接口信息");
                System.out.println();
                while (nifs.hasMoreElements()) {
                    NetworkInterface nif = nifs.nextElement();
                    Enumeration<InetAddress> addresses = nif.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        addr = addresses.nextElement();
                        System.out.println("网卡接口名称：" + nif.getName());
                        System.out.println("网卡接口地址：" + addr.getHostAddress());
                        System.out.println();
                    }
                }
                return null;
            }

        } catch (Exception e) {
            System.out.println("获取Socket失败");
        }
        return null;
    }
}
